package com.easyn.CamLinePro;

import java.lang.Thread.UncaughtExceptionHandler;

import com.easyn.P2PCam264.DatabaseManager;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Looper;

public class AppException implements UncaughtExceptionHandler
{
	public static final String TAG = "CrashHandler";
	private static AppException instance;
	private Context mContext;
	
	private Thread.UncaughtExceptionHandler mDefaultHandler;

	private AppException(Context context)
	{
		init(context);
	}

	public static AppException getInstance(Context context)
	{
		if (instance == null)
		{
			instance = new AppException(context);
		}
		return instance;
	}

	private void init(Context context)
	{
		mContext = context;
		mDefaultHandler = Thread.getDefaultUncaughtExceptionHandler();
		// Thread.setDefaultUncaughtExceptionHandler(this);
	}

	@Override
	public void uncaughtException(Thread thread, Throwable ex)
	{
		if (!handleException(ex) && mDefaultHandler != null)
		{
			mDefaultHandler.uncaughtException(thread, ex);
		} else
		{
			// �˳�
			//AppManager.getAppManager().AppExit(mContext);
		}
	}

	/**
	 * �Զ��������,�ռ�������Ϣ ���ʹ��󱨸�Ȳ������ڴ����. �����߿��Ը���Լ���������Զ����쳣�����߼�
	 * 
	 * @param ex
	 * @return true:������˸��쳣��Ϣ;���򷵻�false
	 */
	private boolean handleException(Throwable ex)
	{
		if(mContext == null) {
			return false;
		}
		
		if (ex == null)
		{
			return true;
		}
		
		final String crashReport = getCrashReport(mContext, ex);
		
		new Thread()
		{
			@Override
			public void run()
			{
				Looper.prepare();
				sendAppCrashReport(mContext, crashReport);
				Looper.loop();
			}
		}.start();
		return true;
	}
	
	/**
	 * ��ȡAPP�����쳣����
	 * @param ex
	 * @return
	 */
	private String getCrashReport(Context context, Throwable ex) {
		PackageInfo pinfo = getPackageInfo();
		
		StringBuffer exceptionStr = new StringBuffer();
		exceptionStr.append("Version: "+pinfo.versionName+"("+pinfo.versionCode+")\n");
		exceptionStr.append("Android: "+android.os.Build.VERSION.RELEASE+"("+android.os.Build.MODEL+")\n");
		exceptionStr.append("Exception: "+ex.getMessage()+"\n");
		StackTraceElement[] elements = ex.getStackTrace();
		for (int i = 0; i < elements.length; i++) {
			exceptionStr.append(elements[i].toString()+"\n");
		}
		DatabaseManager.logfile("getCrashReport="+exceptionStr);
		return exceptionStr.toString();
	}
	
	/**
	 * ��ȡApp��װ����Ϣ
	 * @return
	 */
	public PackageInfo getPackageInfo() {
		PackageInfo info = null;
		try { 
			info = mContext.getPackageManager().getPackageInfo(mContext.getPackageName(), 0);
		} catch (NameNotFoundException e) {    
			e.printStackTrace(System.err);
		} 
		if(info == null) info = new PackageInfo();
		return info;
	}
	
	/**
	 * ����App�쳣��������
	 * 
	 * @param cont
	 * @param crashReport
	 */
	public static void sendAppCrashReport(final Context cont,
			final String crashReport) {
		
		//DataControler.crash(crashReport);
		/*AlertDialog.Builder builder = new AlertDialog.Builder(AppManager.create().topActivity());
		builder.setIcon(android.R.drawable.ic_dialog_info);
		builder.setTitle("Ӧ�ó������");
		builder.setMessage("�ܱ�Ǹ��Ӧ�ó�����ִ��󣬼����˳���\n���ύ���󱨸棬���ǻᾡ���޸�������⣡");
		builder.setPositiveButton("�ύ����",
				new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {
						dialog.dismiss();
						// �����쳣����
						Intent i = new Intent(Intent.ACTION_SEND);
						// i.setType("text/plain"); //ģ����
						i.setType("message/rfc822"); // ���
						i.putExtra(Intent.EXTRA_EMAIL,
								new String[] { "158358144@qq.com" });
						i.putExtra(Intent.EXTRA_SUBJECT,
								"���ϴ(WashCar)- ���󱨸�");
						i.putExtra(Intent.EXTRA_TEXT, crashReport);
						AppManager.create().topActivity().startActivity(Intent.createChooser(i, "���ʹ��󱨸�"));
						// �˳�
						AppManager.create().AppExit(cont);
					}
				});
		builder.setNegativeButton("��Ҫ����",
				new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {
						dialog.dismiss();
						// �˳�
						AppManager.create().AppExit(cont);
					}
				});
		builder.show();*/
	}
}